using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NTTHAO.Common.BL;
using NTTHAO.CommonWeb;

public partial class admin_webparts_URLSecurity_UserSecurityInput : System.Web.UI.UserControl
{
    public NTTHAO.Common.BL.AdminUser _EditingUser = null;
    public DataTable _URLList = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSave.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.btnSave2.Text = LanguageUtility.GetLanguageText("SAVE", Session);

        if (!IsPostBack)
        {
            string strUserID = Request.QueryString["userid"];

            int userID;
            try
            {
                userID = Int32.Parse(strUserID);
                NTTHAO.Common.BL.AdminUser adminUser = new NTTHAO.Common.BL.AdminUser(userID);

                if (adminUser.ID > 0 && adminUser.Type == AdminUser.NOMAL_TYPE)
                {
                    _EditingUser = adminUser;

                    DataSet ds = (new URLMap()).GetAllEditableByUser(_EditingUser.ID);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        this._URLList = ds.Tables[0];
                    }
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                Response.Redirect("?type=ser");
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        _EditingUser = null;
        string strUserID = Request.QueryString["userid"];
        int userID;
        try
        {
            userID = Int32.Parse(strUserID);
            _EditingUser = new NTTHAO.Common.BL.AdminUser(userID);

            if (this._EditingUser.ID < 1)
            {
                throw new Exception();
            }

            // Delete all
            URLSecurity urlSercurity = new URLSecurity();
            urlSercurity.DeleteAllByUserId(this._EditingUser.ID);

            // Insert new
            string urlAllowedListString = Request.Params["chkUrl"];
            if (urlAllowedListString != null && urlAllowedListString.Length > 0)
            {
                string[] urlAllowedsString = urlAllowedListString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < urlAllowedsString.Length; i++)
                {
                    URLSecurity urlSerItem = new URLSecurity();
                    urlSerItem.URLID = Int32.Parse(urlAllowedsString[i].Trim());
                    urlSerItem.UserID = this._EditingUser.ID;
                    urlSerItem.AllowRequest = 1;
                    urlSerItem.Insert();
                }
            }
        }
        catch (Exception)
        {
        }
        finally
        {
            Response.Redirect("?type=ser");
        }
    }
}
